package com.tongjie.hotelbooking.config;

import com.tongjie.hotelbooking.exception.BusinessException;
import com.tongjie.hotelbooking.http.BaseResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

@ControllerAdvice
@Slf4j
public class ExceptionConfig {

    @ExceptionHandler(Exception.class)
    @ResponseBody
    public BaseResponse<String> handleException(Exception e) {
        log.error("internal server error,", e);
        return BaseResponse.<String>builder().code(500).message("internal server error").data(e.getMessage()).build();
    }

    @ExceptionHandler(BusinessException.class)
    @ResponseBody
    public BaseResponse<String> handleException(BusinessException e) {
        log.error("internal server error,", e);
        return BaseResponse.<String>builder().code(e.getCode()).message(e.getMessage()).build();
    }


}
